clear all
set more off

local raw_dir "./data/raw"
local data_dir "./data/cleaned"
local log_dir "./documentation/logs"
local out_dir "./analysis/output/graphs"
local date: display %tdYY-NN-DD date(c(current_date), "DMY")
di "`date'"
capture log close
capture log using `log_dir'/store_maps`date', replace
cd `data_dir'
** make main WIC store map for starting year of sample
shp2dta using `raw_dir'/gis/cb_2016_us_state_500k, data("state_2016_data") coor("state_2016_coord") gencent(coor) genid(stid) replace

use ./state_2016_coord
drop if _ID == 2 | _ID == 11 | _ID == 33 | _ID == 34 | _ID == 54 | _ID == 55 | _ID == 56
save tempcoordl48, replace

use state_2016_data
drop if NAME == "Alaska" | NAME == "American Samoa" | NAME == "Commonwealth of the Northern Mariana Islands" | NAME == "Guam" | NAME == "Hawaii" | NAME == "Puerto Rico" | NAME == "United States Virgin Islands"

spmap using tempcoordl48, id(stid) fcolor(white) ocolor(gs10) osize(vthin) point(data(geocode_tip_auth.dta) select(keep if fiscalyear == 2005 & state_name != "Alaska" & state_name != "Hawaii") x(slong) y(slat) size(tiny) fcolor(blue) osize(none))
graph export `out_dir'/wic_map_2005_l48.png, as(png) replace


mkf tip_stores
cwf tip_stores
use geocode_tip_auth.dta

drop if missing(slat)
rename (slat slong) (latitude longitude)
*consolidate some of the information between the two vendor type variables
replace typeofvendor = "" if typeofvendor == "D" | typeofvendor == "H"
replace vendor_type = 3 if typeofvendoraabove50vend == "R" & missing(vendor_type)
replace typeofvendoraabove50vend = "R" if vendor_type == 3 & missing(typeofvendoraabove50vend)
replace vendor_type = 2 if typeofvendoraabove50vend == "P" & missing(vendor_type)
replace typeofvendoraabove50vend = "P" if vendor_type == 2 & missing(typeofvendoraabove50vend)
replace vendor_type = 1 if typeofvendoraabove50vend == "C" & missing(vendor_type)
replace typeofvendoraabove50vend = "C" if vendor_type == 1 & missing(typeofvendoraabove50vend)
replace vendor_type = 4 if typeofvendoraabove50vend == "A" | typeofvendoraabove50vend == "W"
lab def vendor_type 4 "A50/WIC-only", add
lab val vendor_type vendor_type

tempvar type_mode
bys vendorname fadd: egen `type_mode' = mode(vendor_type)
replace vendor_type = `type_mode' if !missing(`type_mode') & missing(vendor_type)

drop `type_mode'
tempvar type_mode
bys vendorname fadd: egen `type_mode' = mode(typeofvendor)
replace typeofvendor = `type_mode' if !missing(`type_mode') & missing(typeofvendor)

*put each different store type into a tempfile
keep latitude longitude vendor_type fiscalyear typeofvendor
duplicates drop
levelsof(vendor_type), local(vendortypes)
*local label : value label vendor_type
// *put each different store type into a tempfile
// foreach type of local vendortypes{
// 	preserve
// 	keep if vendor_type == `type'
// 	*local lab_`type' : label `label' `type'
// 	*local lab_`type' = lower(subinstr("`lab_`type''", " ", "_"))
// 	drop fiscalyear
// 	duplicates drop
// 	gen id = _n
// 	tempfile wic_vendors_`type'
// 	save `wic_vendors_`type''
// 	restore
// }
// levelsof(typeofvendor), local(vendortypes)
// *put each different store type into a tempfile
// foreach type of local vendortypes{
// 	preserve
// 	keep if typeofvendor == "`type'"
// 	drop fiscalyear
// 	duplicates drop
// 	gen id = _n
// 	tempfile wic_vendors_`type'
// 	save `wic_vendors_`type''
// 	restore
// }
levelsof(fiscalyear), local(years)
foreach year of local years{
	preserve
	keep if fiscalyear == `year'
	duplicates drop
	gen id = _n
	tempfile wic_vendors_`year'
	save `wic_vendors_`year''
	restore
}

mkf snap_stores
cwf snap_stores
use stars_stores_clean.dta
drop if missing(latitude)
keep storetype latitude longitude auth_date term_date 
replace storetype = "BB" if storetype == "Bakery Specialty"
replace storetype = "BC" if storetype == "Food Buying Co-op"
replace storetype = "CO" if storetype == "Combination Grocery/Other"
replace storetype = "CS" if storetype == "Convenience Store"
replace storetype = "DR" if storetype == "Delivery Route"
replace storetype = "FM" if storetype == "Farmers' Market"
replace storetype = "FV" if storetype == "Fruits/Veg Specialty"
replace storetype = "MC" if storetype == "Military Commissary"
replace storetype = "ME" if storetype == "Meat/Poultry Specialty"
replace storetype = "MG" if storetype == "Medium Grocery Store"
replace storetype = "LG" if storetype == "Large Grocery Store"
replace storetype = "SE" if storetype == "Seafood Specialty"
replace storetype = "SG" if storetype == "Small Grocery Store"
replace storetype = "SM" if storetype == "Supermarket"
replace storetype = "SS" if storetype == "Super Store"
replace storetype = "WH" if storetype == "Wholesaler"

levelsof(storetype), local(vendortypes)
foreach type of local vendortypes{
	preserve
	keep if storetype == "`type'"
	drop *_date
	duplicates drop
	gen id = _n
	tempfile snap_vendors_`type'
	save `snap_vendors_`type''
	restore
}
forval year = 2005/2018{
	preserve
	local endyear = `year' + 1
	keep if inrange(auth_date, -999999, date("30sep`endyear'", "DMY")) & inrange(term_date, date("01oct`year'", "DMY"), 999999999)
	gen fiscalyear = `year'
	duplicates drop
	gen id = _n
	tempfile snap_vendors_`year'
	save `snap_vendors_`year''
	restore
}
//
// mkf ewic_imp
// cwf ewic_imp
// use ewic_rollout.dta
//
// mkf pc_imp
// cwf pc_imp
// use packagechng_imp.dta

/*
going to make 4 graphs:
for graphs 1 and 2, pick one year (say 2014) from middle of sample for representative shapefiles at county level.
for graph 1, plot all WIC stores, with different colors for store type
for graph 2, plot all STARS stores, with different colors for store type
for graphs 3 and 4, need to download county boundary shapefiles for each year that is in sample and data is available - use boundaries that are relevant for the particular year
graph 3 will actually be more like a gif - make one picture per fiscalyear, with all the WIC and SNAP stores authorized in that year (different colors/markers for WIC and SNAP) and counties colored by eWIC implementation status
graph 4 is same but for package change implementation (which only moves around 2009/2010)
*/
*graph 1 approximate syntax
cd `raw_dir'/gis
spmap using countycoord.dta, id(_ID) fcolor(white) ocolor(gs10) osize(vthin) ///
point(data(`wic_vendors_P'.dta) x(longitude) y(latitude) size(tiny) fcolor(red) osize(none)) ///
point(data(`wic_vendors_R'.dta) x(longitude) y(latitude) size(tiny) fcolor(blue) osize(none)) ///
point(data(`wic_vendors_A'.dta) x(longitude) y(latitude) size(tiny) fcolor(black) osize(none)) /// 
point(data(`wic_vendors_C'.dta) x(longitude) y(latitude) size(tiny) fcolor(olive) osize(none))

*graph 3 approximate syntax
merge 1:m ct_fips using `raw_data_dir'/ewic/ewicfortiptdlmerge.dta